﻿package view.components.background
{
	import org.casalib.display.CasaSprite;
    import view.components.utils.TrigoBase;
    import gs.TweenLite;
	import gs.easing.*;
    import flash.display.*;
    import flash.geom.*;

	//Create changeEffect
    public class BackgroundStrip extends CasaSprite
    {
        private var _slices:Array;
        private var _bm:BitmapData;
        private var _progress:Number;
        private var _box:Rectangle;

        public function BackgroundStrip($box:Rectangle, $progress:Number, $bm:BitmapData)
        {
            _box = $box;
            _progress = $progress;
            _bm = $bm
            _slices = [];
            buildSlices();
        }

        private function buildSlices() : void
        {
            var delayValue:Number = NaN;
            var slicesArr:Array = null;
            var sliceOneSprite:Sprite = null;
            var sliceTwoSprite:Sprite = null;
            var numSlices:uint = Math.floor(_box.width / 90);
            delayValue = 0;
            while (delayValue < numSlices)
            {
                slicesArr = createSlice(delayValue / numSlices + 1 / numSlices, 1 / numSlices);				
                sliceOneSprite = slicesArr[0];
                addChild(sliceOneSprite);
                sliceTwoSprite = slicesArr[1];
                addChild(sliceTwoSprite);
				
                if (_bm != null)
                {
					sliceTwoSprite.y = -20;
					sliceTwoSprite.alpha = 0;
					TweenLite.to(sliceTwoSprite, 1, { y:0, alpha:1, delay:delayValue * 0.04 + 0.1, ease:Circ.easeOut } );
					
					TweenLite.to(sliceTwoSprite, 0.2, { alpha:.4, delay:delayValue * 0.02 + 1, ease:Circ.easeOut } );
					TweenLite.to(sliceOneSprite, 0.3, { tint:0xffffff, delay:delayValue * 0.04 + 0.1 + 1, ease:Circ.easeOut } );
					TweenLite.to(sliceOneSprite, 0.4, { tint:0x000000, x:20, alpha:0, delay:delayValue * 0.04 + 0.5 +1, ease:Circ.easeOut } );
					TweenLite.to(sliceTwoSprite, 0.2, { alpha:0, x:0, delay:delayValue * 0.04 + 0.5 +1, ease:Circ.easeOut } );
                }
                else
                {
                    sliceOneSprite.alpha = 1;
					
					TweenLite.to(sliceTwoSprite, 1.2, { alpha:0.4, delay:delayValue * 0.02, ease:Circ.easeOut } );
					TweenLite.to(sliceOneSprite, 1.3, { alpha:1, delay:delayValue * 0.04 + 0.1, ease:Circ.easeOut } );
					TweenLite.to(sliceOneSprite, 1.4, { alpha:0, delay:delayValue * 0.04 + 0.5, ease:Circ.easeOut } );
					TweenLite.to(sliceTwoSprite, 1.2, { alpha:0, delay:delayValue * 0.04 + 0.5, ease:Circ.easeOut } );
                }
                delayValue = delayValue + 1;
            }
            return;
        }

        private function createSlice($shiftValueF:Number, $shiftValueS:Number) : Array
        {
            var trigoPointA:* = TrigoBase.getDistance(new Point(_box.x, _box.y), new Point(_box.x, _box.y + _box.height));
            var trigoPointB:* = TrigoBase.getDistance(new Point(_box.x, _box.y + _box.height), new Point(_box.x + _box.width, _box.y + _box.height));
            var trigoPointC:* = TrigoBase.getDistance(new Point(_box.x + _box.width, _box.y), new Point(_box.x, _box.y));
            var trigoSumAC:* = trigoPointA + trigoPointC;
            var _loc_7:* = (trigoPointA + trigoPointC) * $shiftValueF;
            var _loc_8:* = trigoSumAC * $shiftValueS;
            var _loc_9:Array = [];
            var _loc_10:Array = [];
            var _loc_11:* = _loc_7 - _loc_8;
            if (_loc_7 - _loc_8 > trigoPointA)
            {
                _loc_9.push(new Point(_box.x + (_loc_11 - trigoPointA), _box.y));
            }
            else
            {
                _loc_9.push(new Point(_box.x, _box.y + _box.height - _loc_11));
            }
            _loc_11 = _loc_7;
            if (_loc_11 > trigoPointA)
            {
                if (_loc_7 - _loc_8 < trigoPointA)
                {
                    _loc_9.push(new Point(_box.x, _box.y));
                }
				
                _loc_9.push(new Point(_box.x + (_loc_11 - trigoPointA), _box.y));
                _loc_10.push(new Point(_box.x + (_loc_11 - trigoPointA), _box.y));
            }
            else
            {
                _loc_9.push(new Point(_box.x, _box.y + _box.height - _loc_11));
                _loc_10.push(new Point(_box.x, _box.y + _box.height - _loc_11));
            }
            _loc_11 = _loc_7;
            if (_loc_11 > trigoPointB)
            {
                _loc_9.push(new Point(_box.x + _box.width, _box.y + _box.height - (_loc_11 - trigoPointB)));
                _loc_10.push(new Point(_box.x + _box.width, _box.y + _box.height - (_loc_11 - trigoPointB)));
                if (_loc_7 - _loc_8 < trigoPointB)
                {
                    _loc_9.push(new Point(_box.x + _box.width, _box.y + _box.height));
                }
            }
            else
            {
                _loc_9.push(new Point(_box.x + _loc_11, _box.y + _box.height));
                _loc_10.push(new Point(_box.x + _loc_11, _box.y + _box.height));
            }
            _loc_11 = _loc_7 - _loc_8;
			
            if (_loc_11 > trigoPointB)
            {
                _loc_9.push(new Point(_box.x + _box.width, _box.y + _box.height - (_loc_11 - trigoPointB)));
            }
            else
            {
                _loc_9.push(new Point(_box.x + _loc_11, _box.y + _box.height));
            }
            var _loc_12:* = new Sprite();
            if (_bm != null)
            {
                _loc_12.graphics.beginBitmapFill(_bm, null, false);
            }
            else
            {
                _loc_12.graphics.beginFill(0);
            }
            var _loc_13:* = _loc_9[0];
            _loc_12.graphics.moveTo(_loc_13.x, _loc_13.y);
            var _loc_14:uint = 1;
            while (_loc_14 < _loc_9.length)
            {
                _loc_13 = _loc_9[_loc_14];
                _loc_12.graphics.lineTo(_loc_13.x, _loc_13.y);
                _loc_14 = _loc_14 + 1;
            }
            _loc_13 = _loc_9[0];
            _loc_12.graphics.endFill();
			
            var _loc_15:* = new Sprite();
            _loc_15.graphics.lineStyle(1, 0x000000, .6);
            _loc_15.graphics.moveTo(_loc_10[0].x, _loc_10[0].y);
            _loc_15.graphics.lineTo(_loc_10[1].x, _loc_10[1].y);
			
            return [_loc_12, _loc_15];
        }
    }
}
